package cn.rongdao.jrkabao.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.mifengkong.frtools.util.FRDisplayMetrics;

/**
 * Created by Wang on 17/9/18.
 */

public class CustomCircleView extends View {
    //圆形进度的中间数字的大小
    public static final int CIRCLE_CENTER_TEXTSIZE = 18;
    //默认最小的宽度
    private int defaultWidth = FRDisplayMetrics.dp2px(getContext(), 180);
    //默认的高度
    private int defaultHeight = FRDisplayMetrics.dp2px(getContext(), 180);
    //圆弧的宽度
    private float defaultArcWidth = FRDisplayMetrics.dp2px(getContext(), 2.5f);
    //小圆点的半径
    private int defaultSmallCircleWidth = FRDisplayMetrics.dp2px(getContext(), 6);
    //字体的大小
    private int textSize = FRDisplayMetrics.dp2px(getContext(), 10);
    //中间字体的大小
    private int centerTextSize = FRDisplayMetrics.dp2px(getContext(), 30);

    //背景半圆的颜色
    private int bgColor = Color.parseColor("#7fffffff");
    //圆形进度的颜色
    private int progressColor = Color.parseColor("#ffffff");
    //三个角标的颜色
    private int pointTextColor = Color.parseColor("#4cffffff");
    //中间字体的颜色
    private int centerTextColor = Color.parseColor("#ffffff");

    //字体和圆弧的padding
    private int padding = FRDisplayMetrics.dp2px(getContext(), 5);
    //距离圆弧的距离
    private int margin = (int) Math.max(defaultSmallCircleWidth, textSize);
    //开始时的角度
    private float currentAngle = 0;
    //记录中间数字
    private String centerTextNumber = "";
    //设置最大的区间
    private float maxCount = 100;
    //开始的角度
    private float startAngle = 180;
    //最后绘制的角度
    private float lastAngle = 180;
    //动画执行的速度
    private int animaSpeed = 1000;
    //开始的数字
    private String startNumber = "";
    //中间的数字
    private String centerNumber = "";
    //结束的数字
    private String endNumber = "";
    //是否需要进度里面的小圆圈
    private boolean needSmallCircle = true;

    private int centerTextMarginTop = 0;

    private Paint paintBg;

    private Paint linePaint;

    private Paint textPaint;

    private Paint smallCirclePaint;

    public CustomCircleView(Context context) {
        super(context);
        initPaint();
    }

    public CustomCircleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    public CustomCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    private void initPaint() {
        paintBg = new Paint();
        linePaint = new Paint();
        textPaint = new Paint();
        smallCirclePaint = new Paint();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int resultWidth = widthSize;
        int resultHeight = heightSize;
        if (widthMode == MeasureSpec.AT_MOST) {
            resultWidth = defaultWidth;
        }
        if (heightMode == MeasureSpec.AT_MOST) {
            resultHeight = defaultHeight;
        }
        setMeasuredDimension(resultWidth, resultHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        /***************绘制背景的圆弧******************/
        paintBg.setColor(bgColor);
        paintBg.setStrokeCap(Paint.Cap.SQUARE);
        float borderWidth = defaultArcWidth;
        float centerX = (getWidth() - 2 * margin) / 2;
        RectF ovalBg = new RectF(margin, margin, 2 * centerX + margin, 2 * centerX + margin);
        //Paint.Style.FILL    :填充内部
        //Paint.Style.FILL_AND_STROKE  ：填充内部和描边
        //Paint.Style.STROKE  ：仅描边
        paintBg.setStyle(Paint.Style.STROKE);
        //抗锯齿
        paintBg.setAntiAlias(true);
        //画笔的宽度
        paintBg.setStrokeWidth(borderWidth);

        canvas.drawArc(ovalBg, 180, 180, false, paintBg);
        /***************绘制进度的圆弧*****************/
        Paint currentPaint = new Paint();
        currentPaint.setStyle(Paint.Style.STROKE);
        currentPaint.setAntiAlias(true);
        currentPaint.setStrokeWidth(borderWidth);
        currentPaint.setColor(progressColor);
        currentPaint.setStrokeCap(Paint.Cap.SQUARE);

        canvas.drawArc(ovalBg, startAngle, currentAngle, false, currentPaint);
        /***************绘制内部的圆弧*****************/

        linePaint.setColor(Color.parseColor("#50ffffff"));
        linePaint.setAntiAlias(true);
        linePaint.setStrokeWidth(FRDisplayMetrics.dp2px(getContext(), 1));
        //半径
        float radius = centerX - FRDisplayMetrics.dp2px(getContext(), 6.5f);
        //X轴中点坐标
        float pointX = centerX + FRDisplayMetrics.dp2px(getContext(), 9.5f);

        for (float i = 90; i < 271; i += 4) {
            double rad = i * Math.PI / 180;
            float startX = (float) (pointX + (radius - FRDisplayMetrics.dp2px(getContext(), 3)) * Math.sin(rad));
            float startY = (float) (pointX + (radius - FRDisplayMetrics.dp2px(getContext(), 3)) * Math.cos(rad));

            float stopX = (float) (pointX + radius * Math.sin(rad) + 1);
            float stopY = (float) (pointX + radius * Math.cos(rad) + 1);

            canvas.drawLine(startX, startY, stopX, stopY, linePaint);
        }
        /***************绘制角标文字*****************/

        textPaint.setTextSize(textSize);
        textPaint.setColor(pointTextColor);
        textPaint.setAntiAlias(true);
        textPaint.setTextAlign(Paint.Align.CENTER);

        canvas.drawText(startNumber, margin, centerX + margin + textSize + padding, textPaint);
        canvas.drawText(centerNumber, getWidth() / 2, (int) (getWidth() * 0.2), textPaint);
        canvas.drawText(endNumber, getWidth() - margin, centerX + margin + textSize + padding, textPaint);
        /***************绘制中间进度文字*****************/
        textPaint.setTextSize(centerTextSize);
        textPaint.setColor(centerTextColor);
//        Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
//        textPaint.setTypeface(font);
        canvas.drawText(centerTextNumber, getWidth() / 2, (int) (getWidth() * 0.42) + centerTextMarginTop, textPaint);
        /***************绘制小圆*****************/
        if (needSmallCircle) {
            canvas.translate(getWidth() / 2, getWidth() / 2);

            smallCirclePaint.setStyle(Paint.Style.FILL);
            smallCirclePaint.setAntiAlias(true);
            smallCirclePaint.setColor(centerTextColor);
            //旋转画布 画小圆
            canvas.rotate(currentAngle);
            canvas.drawCircle(-centerX, 0, defaultSmallCircleWidth, smallCirclePaint);
            canvas.rotate(-currentAngle);
        }

    }

    /*设置是否显示小圆圈*/
    public void setNeedSmallCircle(boolean needSmallCircle) {
        this.needSmallCircle = needSmallCircle;
    }

    /*设置中间字体当前位置距离顶部的距离*/
    public void setCenterTextMarginTop(int centerTextMarginTop) {
        this.centerTextMarginTop = centerTextMarginTop;
    }

    /*设置开始的数字指示*/
    public void setStartNumber(String startNumber) {
        this.startNumber = startNumber;
    }

    /*设置中间的数字指示*/
    public void setCenterNumber(String centerNumber) {
        this.centerNumber = centerNumber;
    }

    /*设置右边的数字指示*/
    public void setEndNumber(String endNumber) {
        this.endNumber = endNumber;
    }

    /*设置中间的进度文字,某种情况需要*/
    public void setCenterTextNumber(String centerTextNumber, int testSize) {
        this.centerTextSize = FRDisplayMetrics.dp2px(getContext(), testSize);
        this.centerTextNumber = centerTextNumber;
        invalidate();
    }

    /*设置中间的进度文字,某种情况需要*/
    public void setCenterTextNumber(String centerTextNumber) {
        setCenterTextNumber(centerTextNumber, 28);
    }

    /*设置背景圆弧的宽度*/
    public void setDefaultArcWidth(float defaultArcWidth) {
        this.defaultArcWidth = defaultArcWidth;
    }

    /*设置默认的宽度*/
    public void setDefaultWidth(int defaultWidth) {
        this.defaultWidth = FRDisplayMetrics.dp2px(getContext(), defaultWidth);
    }

    /*设置默认的高度*/
    public void setDefaultHeight(int defaultHeight) {
        this.defaultHeight = FRDisplayMetrics.dp2px(getContext(), defaultHeight);
    }

    /*设置半圆的进度*/
    public void setCurrentAngle(float currentAngle) {
        float curren = currentAngle / maxCount;
        float last_angle = 180 * (curren > 1 ? 1 : curren);
        centerTextNumber = String.valueOf((int) (currentAngle * 1000));
        //这里可以设置动画的起点在哪里
        lastAngle = 0;
        setAnimation(lastAngle, last_angle, animaSpeed);
    }

    /**
     * 为进度设置动画
     *
     * @param last    变化范围的开始
     * @param current 变化范围的结束
     */
    private void setAnimation(float last, float current, int length) {
        ValueAnimator progressAnimator = ValueAnimator.ofFloat(last, current);
        progressAnimator.setDuration(length);
        progressAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                currentAngle = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
        progressAnimator.start();
    }

}